<template>
	<view :data-theme="theme">
		<view class="card">
			<view style="display: flex;padding: 20rpx;justify-content: space-between;">
				<view class="title">
					结算类型
				</view>
				<view class="title-c">
					<radio-group @change="radioChange" style="display: flex;flex-flow: row;">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
							<view style="padding: 0 20rpx;">
								<!-- <radio :value="item.value" :checked="index === current" /> -->
								<view style="display: inline-block;">{{item.name}}</view>
							</view>
						</label>
					</radio-group>
				</view>
			</view>
			<view class="form-content" style="margin: 0 20rpx;">
				<u--form labelPosition="left" :model="addSettleForm" :rules="rules" ref="uForm">
					<u-form-item label="开户户名" prop="bankUserName" :labelWidth="labelWidth" borderBottom ref="item1">
						<u--input v-model="addSettleForm.bankUserName" border="none" placeholder="请输入开户户名"></u--input>
					</u-form-item>
					<u-form-item label="银行名称" prop="bankName" :labelWidth="labelWidth" borderBottom ref="item1">
						<u--input v-model="addSettleForm.bankName" placeholder="请输入银行名称" border="none"></u--input>
					</u-form-item>
					<u-form-item label="银行卡号码" prop="bankCard" :labelWidth="labelWidth" borderBottom ref="item1">
						<u--input v-model="addSettleForm.bankCard" placeholder="请输入银行卡号码" border="none"></u--input>
					</u-form-item>
					<u-form-item label="银行地址" prop="bankAddress" :labelWidth="labelWidth" ref="item1">
						<u--input v-model="addSettleForm.bankAddress" placeholder="请输入银行地址" border="none"></u--input>
					</u-form-item>
				</u--form>
			</view>

		</view>
		<view class="footer" style="position: fixed;bottom: 0;left: 0;right: 0;display: flex;margin: 20rpx;">
			<button class="bg_color" style="padding: 20rpx;color: #fff;font-weight: bold;flex: 1;"
				@click="updateSettlement()">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getSettlementApi,
		updateSettlementEditApi
	} from '@/api/finance.js';
	import {
		toApplyClosingApi
	} from '@/api/finance.js';
	import {
		setThemeColor
	} from '../../../utils/setTheme';
	let app = getApp();
	export default {
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				addSettleForm: {
					settlementType: "bank",
					bankAddress: '',
					bankCard: '',
					bankName: '',
					bankUserName: '',
				},
				items: [{
						value: 'bank',
						name: '银行卡',
						checked: 'true'
					},
					// {
					// 	value: 'wechat',
					// 	name: '微信'
					// },
					// {
					// 	value: 'alipay',
					// 	name: '支付宝'
					// }
				],
				current: 0, //结算类型索引
				// 校验
				rules: {
					bankUserName: [{
						type: 'string',
						required: true,
						message: '请输入开户户名',
						trigger: ['blur', 'change']
					}],
					bankName: [{
						type: 'string',
						required: true,
						message: '请输入银行名称',
						trigger: ['blur', 'change']
					}],
					bankCard: [{
						type: 'string',
						required: true,
						message: '请输入银行卡号',
						trigger: ['blur', 'change']
					},{
						pattern:/^([1-9]{1})(\d{15}|\d{18})$/,
						transform(value){
							return String(value);
						},
						message:'请输入正确的19位数银行卡号'
					}],
					bankAddress: {
						type: 'string',
						required: true,
						message: '请输入银行地址',
						trigger: ['blur', 'change']
					}
				},
				labelWidth: '160rpx',
			};
		},
		onLoad() {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
			this.getSettlement();
		},
		methods: {
			//获取结算信息
			getSettlement() {
				getSettlementApi().then((res) => {
					this.addSettleForm = {
						settlementType: "bank",
						bankAddress: res.data.bankAddress,
						bankCard: res.data.bankCard,
						bankName: res.data.bankName,
						bankUserName: res.data.bankUserName,
					};
				})
			},
			// 提交结算
			updateSettlement() {
				this.$refs.uForm.validate().then(res => {
					updateSettlementEditApi(this.addSettleForm).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '提交成功',
								icon: 'success', // 提示图标，可选值：'success', 'loading', 'none'
								duration: 2000 // 持续时间，单位为毫秒
							});
							setTimeout(() => {
								const pages =
									getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
								const prevPage = pages[pages.length - 2]; // 获取上一级页面对象
								uni.navigateBack({
									delta: 1,
								});
							}, 1000)

						}
					})

				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			// 所选结算类型
			radioChange(evt) {
				this.applyForm.settlementType = evt.detail.value;
			}
		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}

	.card {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 10rpx;
	}

	.column {
		padding: 20rpx;
		display: flex;
		flex-flow: row;
		text-align: center;
		justify-content: space-between;

		.title {
			display: flex;
			// flex: 1;
			width: 150rpx;
		}

		.title-c {
			display: flex;
			flex: 1;
			justify-content: left;
			// padding-left: 20rpx;
		}
	}
</style>